from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction        # 手势操作用这个模块
import allure
import pytest
import time
@allure.feature("存钱罐：APP")
class TestCun:
    @allure.story("前置：驱动")
    @pytest.fixture()
    def driver(self):
        # 初始化参数
        cap_dict = {}
        cap_dict['platformName'] = 'Android'  # 平台系统：Android  或者是iOS
        cap_dict['platformVersion'] = '7.1.2'  # 系统版本
        cap_dict['deviceName'] = '127.0.0.1:62001'  # 设备名称：可以通过 adb devices -l 查看
        cap_dict['appPackage'] = 'com.lingyue.zebraloan'  # 包名：可以通过 adb shell dumpsys activity activities
        cap_dict['appActivity'] = 'com.lingyue.banana.activities.BananaLaunchActivity'
        # 初始化dirver
        driver = webdriver.Remote("http://localhost:4723/wd/hub", cap_dict)
        # 设置隐式等待
        driver.implicitly_wait(15)
        yield driver
        # 等待结束页面
        time.sleep(10)
    @allure.story("用户故事测试")
    def test_userStory(self,driver):
        self.driver = driver
        # 点击同意按钮
        self.driver.find_element(MobileBy.ID, "com.lingyue.zebraloan:id/tv_confirm").click()
        # 等待页面跳转
        time.sleep(10)
        # 点击用户故事
        self.driver.find_elements(MobileBy.CLASS_NAME, "android.widget.ImageView")[5].click()
        # 等待页面跳转
        time.sleep(5)
        # 用手指点击
        TouchAction(self.driver).tap(x=294, y=550).perform()
        """
        tap：点击x值和y值
        perform：执行这一组动作
        """
        # 等待页面跳转
        time.sleep(4)
        # 552 1497     581 363
        TouchAction(self.driver).press(x=552, y=1497).move_to(x=581, y=363).release().perform()
        """
        press:从这个位置开始滑动
        move_to：滑动到这里结束
        release：松开手指
        perform：执行这一组动作
        """
        # 点击返回按钮
        self.driver.find_element(MobileBy.ID, "com.lingyue.zebraloan:id/tb_toolbar").click()
        # 断言有没有回到首页
        time.sleep(2)
        text = self.driver.find_element(MobileBy.XPATH, "//*[@text='品牌介绍']").text
        assert text == '品牌介绍'



# pytest [测试文件] --alluredir=allure
# allure generate ./allure/ -o ./reports --clean




